﻿#region Creditos

/* Autor: Edilberto Sánchez Forero	*/
/* Email: esanchezfo@gmail.com		*/
/* Licencia: Propietario			*/
/* País: Colombia, Bogotá			*/

#endregion

#region Librerias

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ESF.General.Entity;
using System.Data;
using ESF.General;
using ESF.SSO.Core.Entities;
using ESF.General.Text;
using ESF.SSO.Core.DataAccess.BasicReferences;
using ESF.SSO.Core.Entities.BasicReferences;

#endregion


namespace ESF.SSO.Core.DataAccess
{
	public static class AdoEnterprise
	{
		#region Read

		public static List<Enterprise> List(string pPartName = null, int? pListCount = null)
		{
			IDbCommand cmd;
			
		
			cmd = AdoESFSSO.R.CreateSPCommand ("esf_sso.P_ListEnterprises");
			
			AdoESFSSO.R.AddParameter (cmd, "pPartName", pPartName);
			AdoESFSSO.R.AddParameter (cmd, "pListCount", pListCount);
			
			return AdoESFSSO.R.GetList <Enterprise> (cmd, ConvertTo);
		}
		
		
		public static Enterprise ConvertTo (IDataReader pReader, string pPrefix = "", string pListLoading = "")
		{
			Enterprise result;
			
			
			result = AdoBasicReference.ConvertToLocated<Enterprise> (pReader, pPrefix + "Ent_");
			
			result.IdentificationNumber = DataHelper.GetString (pReader, pPrefix + "Ent_IdentificationNumber");
			result.IdentificationNumberDigit = DataHelper.GetString (pReader, pPrefix + "Ent_IdentificationNumberDigit");
			
			result.IdentificationType = DataHelper.ConvertToDirectBasicReference (pReader, pPrefix + "Ent_IdType_");
			
			result.Terminals = AdoTerminal.GetTerminals<Enterprise> (result, false).ToList ();

			return result;
		}
		
		
		public static Enterprise GetByName (Enterprise pEnterprise)
		{
			IDbCommand cmd;
			
			
			cmd = AdoESFSSO.R.CreateSPCommand ("esf_sso.P_GetEnterpriseByName");
			
			AdoESFSSO.R.AddParameter (cmd, "pName", pEnterprise.Name);
			
			return AdoESFSSO.R.Get <Enterprise> (cmd, ConvertTo);
		}


		public static Enterprise GetByCode(Enterprise pEnterprise)
		{
			IDbCommand cmd;


			cmd = AdoESFSSO.R.CreateSPCommand("esf_sso.P_GetEnterpriseByCode");

			AdoESFSSO.R.AddCodeParameter(cmd, "pCode", pEnterprise);

			return AdoESFSSO.R.Get<Enterprise>(cmd, ConvertTo);
		}
		
		
		public static Enterprise Get (Enterprise pEnterprise)
		{
			IDbCommand cmd;
			
			
			cmd = AdoESFSSO.R.CreateSPCommand ("esf_sso.P_GetEnterprise");
			
			AdoESFSSO.R.AddIDParameter (cmd, "pEnterpriseID", pEnterprise.ID);
			AdoESFSSO.R.AddParameter (cmd, "pIdentificationNumber", pEnterprise.IdentificationNumber);
			AdoESFSSO.R.AddParameter(cmd, "pIdentificationNumberDigit", pEnterprise.IdentificationNumberDigit);
			AdoESFSSO.R.AddCodeParameter(cmd, "pIdentificationTypeCode", pEnterprise.IdentificationType);

			return AdoESFSSO.R.Get <Enterprise> (cmd, ConvertTo);
		}
		
		
		public static BrowseResults<Enterprise> Browse (Enterprise pSample, PaggingInfo pPagInfo)
		{
			IDbCommand cmd;
			
			
			cmd = AdoESFSSO.R.CreateSPCommand ("esf_sso.P_BrowseEnterprises");
			
			AdoESFSSO.R.AddIDParameter (cmd, "pEnterpriseID", pSample.ID);
			AdoESFSSO.R.AddParameter (cmd, "pName", pSample.Name, ComparisonTypes.LikeFull);
			AdoESFSSO.R.AddParameter (cmd, "pIdentificationNumber", pSample.IdentificationNumber, ComparisonTypes.LikeFull);
			AdoESFSSO.R.AddIDParameter (cmd, "pIdentificationTypeID", pSample.IdentificationType);
			
			return AdoESFSSO.R.GetBrowse <Enterprise> (cmd, ConvertTo, pPagInfo);
		}
		
		#endregion
		
		
		#region Write
		
		public static void Delete (Enterprise pEnterprise)
		{
			IDbCommand cmd;
			
			
			cmd = AdoESFSSO.W.CreateSPCommand ("esf_sso.P_DeleteEnterprise");
			
			AdoESFSSO.W.AddIDParameter (cmd, "pEnterpriseID", pEnterprise);
			
			AdoESFSSO.W.Execute (cmd);
		}
		
		
		public static int Register (Enterprise pEnterprise)
		{
			IDbCommand cmd;
			
			
			if (pEnterprise.ID == null)
			{
				cmd = AdoESFSSO.W.CreateSPCommand ("esf_sso.P_InsertEnterprise");
				
				AdoESFSSO.W.AddOutParameter (cmd, "pEnterpriseID", DbType.Int32);
				AdoESFSSO.W.AddAuditableEntityParamsCreation (cmd, pEnterprise);
			}
			else
			{
				cmd = AdoESFSSO.W.CreateSPCommand ("esf_sso.P_UpdateEnterprise");
			
				AdoESFSSO.W.AddIDParameter (cmd, "pEnterpriseID", pEnterprise);
				AdoESFSSO.W.AddAuditableEntityParamsUpdate (cmd, pEnterprise);
			}
			
			AdoESFSSO.W.AddParameter (cmd, "pIdentificationNumber", pEnterprise.IdentificationNumber);
			AdoESFSSO.W.AddParameter (cmd, "pIdentificationNumberDigit", pEnterprise.IdentificationNumberDigit);			
			AdoESFSSO.W.AddCodeParameter (cmd, "pIdentificationTypeCode", pEnterprise.IdentificationType);
			AdoESFSSO.W.AddParameter (cmd, "pName", pEnterprise.Name);
			AdoESFSSO.W.AddParameter (cmd, "pDescription", pEnterprise.Description);
			AdoESFSSO.W.AddParameter (cmd, "pIsEnabled", pEnterprise.IsEnabled);
			AdoESFSSO.W.AddParameter (cmd, "pCode", Guid.NewGuid ().ToString ());

			
			AdoESFSSO.W.Execute (cmd);
			
			if (pEnterprise.ID == null)
			{
				pEnterprise.ID = AdoESFSSO.W.GetParameterValue <int?> (cmd, "pEnterpriseID");
			}

			
			pEnterprise.Location.ID = AdoBasicReference.RegisterLocated (pEnterprise);

			AdoBasicReference.DeleteChildrenBasicReference<Enterprise> (null, pEnterprise, (int) BasicReferenceTypes.Terminals);

			if (!DataHelper.IsEmpty (pEnterprise.Terminals))
			{
				foreach (Terminal ter in pEnterprise.Terminals)
				{
					if (ter.ID == null)
					{
						ter.ID = AdoBasicReference.Register (ter);
					}

					AdoBasicReference.RegisterByCodes (new TreeBasicReference ()
					{
						Child = ter,
						Parent = pEnterprise
					});
				}
			}

			return pEnterprise.ID.Value;
		}
		
		#endregion
	}
}
